package activity.local;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class ActivityActivity extends Activity implements OnClickListener {
	
	Button btcalc;
	EditText op1, op2;
	TextView resultado;
	ArrayAdapter adapter;
	Spinner s;
	
	public static final int OP_SUM = 0;
	public static final int OP_MIN = 1;
	public static final int OP_MUL = 2;
	public static final int OP_DIV = 3;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btcalc = (Button) findViewById(R.id.button1);
        btcalc.setOnClickListener(this);
        
        op1 = (EditText) findViewById(R.id.editText1);
        op2 = (EditText) findViewById(R.id.editText2);
        
        resultado = (TextView) findViewById(R.id.textView4);
        
        s = (Spinner) findViewById(R.id.spinner1);
        
        adapter = ArrayAdapter.createFromResource(this, R.array.itens, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
    }

	public void onClick(View v) {
		Socket socket;

		if(v==btcalc){
			int a=Integer.parseInt(op1.getText().toString());
			int b=Integer.parseInt(op2.getText().toString());
			String operacao = (String) s.getSelectedItem();
			char c = operacao.charAt(0);
			DataInputStream in;
			DataOutputStream out;
			
			switch(c){
				case '+':
				try {
					socket = new Socket("10.0.2.2",8080);
					in  = new DataInputStream(socket.getInputStream());
					out = new DataOutputStream(socket.getOutputStream());
					out.writeInt(OP_SUM);
					out.writeInt(a);
					out.writeInt(b);
					int result = in.readInt();
					resultado.setText(Integer.toString(result));
					
					socket.close();
					
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
					break;
					
				case '-':
					try {
						socket = new Socket("10.0.2.2",8080);
						in  = new DataInputStream(socket.getInputStream());
						out = new DataOutputStream(socket.getOutputStream());
						out.writeInt(OP_MIN);
						out.writeInt(a);
						out.writeInt(b);
						int result = in.readInt();
						resultado.setText(Integer.toString(result));
						
						socket.close();
						
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					break;
					
				case '*':
					try {
						socket = new Socket("10.0.2.2",8080);
						in  = new DataInputStream(socket.getInputStream());
						out = new DataOutputStream(socket.getOutputStream());
						out.writeInt(OP_MUL);
						out.writeInt(a);
						out.writeInt(b);
						int result = in.readInt();
						resultado.setText(Integer.toString(result));
						
						socket.close();
						
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					break;
					
				case '/':
					if(b!=0){
						try {
							socket = new Socket("10.0.2.2",8080);
							in  = new DataInputStream(socket.getInputStream());
							out = new DataOutputStream(socket.getOutputStream());
							out.writeInt(OP_DIV);
							out.writeChars("teste");
							out.writeInt(a);
							out.writeInt(b);
							int result = in.readInt();
							resultado.setText(Integer.toString(result));
							
							socket.close();
							
						} catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
					} else resultado.setText("Div by zero");
					break;
					
				default:
					break;
			}			
		}
	}
}